// src/utils/theme.ts

// 定义主题配置接口
interface ThemeConfig {
    name: string;
    start: string;
    end: string;
    primary: string;
    tabBarSelectedColor: string;
}

// 定义所有主题的类型
interface Themes {
    [key: string]: ThemeConfig;
}

// 中国风主题配置
const themes: Themes = {
    purple: {
        name: '紫气东来',
        start: '#8e44ad',
        end: '#9b59b6',
        primary: '#8e44ad',
        tabBarSelectedColor: '#9b59b6'
    },
    red: {
        name: '中国红',
        start: '#c0392b',
        end: '#e74c3c',
        primary: '#c0392b',
        tabBarSelectedColor: '#e74c3c'
    },
    blue: {
        name: '青花瓷',
        start: '#2980b9',
        end: '#3498db',
        primary: '#2980b9',
        tabBarSelectedColor: '#3498db'
    },
    green: {
        name: '竹林清风',
        start: '#16a085',
        end: '#1abc9c',
        primary: '#16a085',
        tabBarSelectedColor: '#1abc9c'
    },
    gold: {
        name: '金桂飘香',
        start: '#d35400',
        end: '#f39c12',
        primary: '#d35400',
        tabBarSelectedColor: '#f39c12'
    },
    ink: {
        name: '水墨丹青',
        start: '#34495e',
        end: '#2c3e50',
        primary: '#34495e',
        tabBarSelectedColor: '#2c3e50'
    }
};

/**
 * 获取当前主题
 * @returns {ThemeConfig} 当前主题配置
 */
export function getCurrentTheme(): ThemeConfig {
    try {
        const themeKey: string = uni.getStorageSync('appTheme') || 'purple';
        return themes[themeKey] || themes.purple;
    } catch (e) {
        return themes.purple;
    }
}

/**
 * 切换主题
 * @param {string} themeKey 主题键名
 * @returns {boolean} 是否切换成功
 */
export function switchTheme(themeKey: string): boolean {
    if (themes[themeKey]) {
        try {
            uni.setStorageSync('appTheme', themeKey);

            // #ifdef APP-PLUS
            // 同步更新 tabBar 颜色
            const theme = themes[themeKey];
            if (theme && theme.tabBarSelectedColor) {
                plus.tabBar.setTabBarStyle({
                    selectedColor: theme.tabBarSelectedColor
                });
            }
            // #endif

            return true;
        } catch (e) {
            console.error('切换主题失败:', e);
            return false;
        }
    }
    return false;
}

/**
 * 获取所有可用主题
 * @returns {Themes} 所有主题配置
 */
export function getAllThemes(): Themes {
    return themes;
}

/**
 * 获取当前主题的渐变色
 * @returns {string} 渐变色字符串
 */
export function getCurrentGradient(): string {
    const theme = getCurrentTheme();
    return `background: linear-gradient(135deg, ${theme.start} 0%, ${theme.end} 100%);`;
}

/**
 * 初始化 tabBar 主题颜色
 * 在应用启动时调用
 */
export function initTabBarTheme(): void {
    // #ifdef APP-PLUS
    try {
        const currentTheme = getCurrentTheme();
        if (currentTheme && currentTheme.tabBarSelectedColor) {
            plus.tabBar.setTabBarStyle({
                selectedColor: currentTheme.tabBarSelectedColor
            });
        }
    } catch (e) {
        console.error('初始化 tabBar 主题失败:', e);
    }
    // #endif
}

/**
 * 获取 tabBar 样式配置
 * @returns {Object} tabBar 样式对象
 */
export function getTabBarStyle(): Object {
    const currentTheme = getCurrentTheme();
    return {
        color: '#999999',
        selectedColor: currentTheme.tabBarSelectedColor || '#007AFF',
        backgroundColor: '#FFFFFF',
        borderStyle: 'black'
    };
}